package com.aspsine.multithreaddownload.demo.ui;

import com.aspsine.multithreaddownload.ResourceTable;
import com.aspsine.multithreaddownload.demo.entity.AppInfo;
import com.aspsine.multithreaddownload.demo.listener.OnItemClickListener;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

public class ListItemProvider extends BaseItemProvider{

    private List<AppInfo> contactData;

    private Context mContext;

    private OnItemClickListener mListener;

    public void setOnItemClickListener(OnItemClickListener listener){
        mListener = listener;
    }

    public ListItemProvider(Context context,List<AppInfo> list) {
        super();
        mContext = context;
        contactData = list;
    }

    @Override
    public int getCount() {
        return contactData.size();
    }

    @Override
    public Object getItem(int i) {
        return contactData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        Component component1 = getItemComponent(i);
        return component1;
    }

    private Image ivImage;
    private Text tVname;
    private Text tvDownloadPerSize;
    private Text tvStatus;
    private Button btnDownload;
    private ProgressBar progressBar;

    public Component getItemComponent(int position) {
        Component container = getComponent(position);
        return container;
    }

    public Component getComItem(int pos){
        Component container = LayoutScatter.getInstance(mContext).parse(
                ResourceTable.Layout_recycler_view, null, false);
        return container;
    }
    private Component getComponent(int position) {
        AppInfo contact = contactData.get(position);
        Component container = LayoutScatter.getInstance(mContext).parse(
                ResourceTable.Layout_recycler_view, null, false);

        tVname = (Text) container.findComponentById(ResourceTable.Id_tvName);
        tVname.setText(contact.getName());

        ivImage = (Image) container.findComponentById(ResourceTable.Id_ivIcon);
        if(position == 0) {//TODO hos API不支持 Picasso.with 导致网络图片无法加载
            ivImage.setImageAndDecodeBounds(ResourceTable.Media_362);
        }

        tvDownloadPerSize = (Text)container.findComponentById(ResourceTable.Id_tvDownloadPerSize);
        tvDownloadPerSize.setText(contact.getDownloadPerSize());

        tvStatus = (Text)container.findComponentById(ResourceTable.Id_tvStatus);
        tvStatus.setText(contact.getStatusText());

        progressBar = (ProgressBar)container.findComponentById(ResourceTable.Id_progressBar);
        progressBar.setProgressValue(contact.getProgress());

        btnDownload = (Button)container.findComponentById(ResourceTable.Id_btnDownload);
        btnDownload.setText(contact.getButtonText());

        btnDownload.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if(null != mListener){
                    mListener.onItemClick(component,position,contact);
                }
            }
        });

        return container;
    }
}
